package com.qfedu.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.apache.commons.dbutils.QueryRunner;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.Scope;

import javax.sql.DataSource;

/**
 * @ClassName DruidConfig
 * @Description TODO
 * @Author Zbt
 * @Date 2022/8/30 21:17
 * @Version 1.0
 **/
@PropertySource(value = "classpath:druid.properties")
public class DruidConfig {
    @Value("${driverClass}")   //expression包 支持el表达式
    private String driverClass;
    @Value("${jdbcUrl}")
    private String jdbcUrl;
    @Value("${user}")
    private String user;
    @Value("${password}")
    private String password;


    @Bean(name = "runner")
    //将当前方法的返回值注入到IOC容器中
    @Scope("prototype")
    public QueryRunner queryRunner(DataSource dataSource){
        QueryRunner queryRunner = new QueryRunner(dataSource);
        return queryRunner;
    }
    @Bean
    public DataSource dataSource(){
        DruidDataSource druidDataSource = new DruidDataSource();
        try {
            druidDataSource.setDriverClassName(driverClass);
            druidDataSource.setUrl(jdbcUrl);
            druidDataSource.setUsername(user);
            druidDataSource.setPassword(password);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return druidDataSource;
    }

}